<?php
namespace app\admin\controller;

use app\admin\model\Auth as ModelAuth;
 
class Auth extends Base
{
    public function index()
    {
        $this->logs('[管理员]权限列表视图');
        $list = ModelAuth::list();
        return view('index' , ['list' => $list]);
    }
    public function update()
    {
        $this->logs('[管理员]更新权限');
        $param = input();
        $res = ModelAuth::update($param);
        if($res) return success();
        return error();
    }
    public function add()
    {
        $this->logs('[管理员]添加权限Url视图');
        $list = ModelAuth::list();
        return view('add' , ['list' => $list]);
    }
    public function doAdd()
    {
        $this->logs('[管理员]添加权限');
        $param = input();
        if(empty(@$param['url']) && empty(@$param['description'])) return error('缺少请求参数');
        $hasUrl = ModelAuth::where('url' , $param['url'])->findOrEmpty()->isEmpty();
        $hasDescription = ModelAuth::where('description' , $param['description'])->findOrEmpty()->isEmpty();
        if(!$hasUrl) return error('已存在该请求地址，请更换');
        if(!$hasDescription) return error('已存在该描述，请更换');
        $res = ModelAuth::create($param);
        if($res) return success();
        return error();
    }
    public function del()
    {
        $this->logs('[管理员]删除权限');
        $id = input('id');
        $res = ModelAuth::destroy($id);
        if($res) return success();
        return error();
    }
}
